package com.ge.med.cchelper.popup.job;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.ge.med.cchelper.Constants;
import com.ge.med.cchelper.bean.CSession;
import com.ge.med.cchelper.bean.Response;
import com.ge.med.cchelper.form.DetailVobForm;
import com.ge.med.cchelper.util.LogUtil;
import com.ge.med.cchelper.util.PropHelper;
import com.ge.med.cchelper.util.ReourceHelper;
import com.ge.med.cchelper.util.SSLhelper;

public class FetchDirJob extends BaseJob {

	String findCmd;
	String folderFullOSPath;

	public FetchDirJob(String message) {
		this(message, null);
	}

	public FetchDirJob(String message, String folderFullOSPath) {
		super(message);
		String vobroot = csession.getVobRoot();
		this.folderFullOSPath = folderFullOSPath;
		// findCmd = "find " + vobroot + "/* -type l -o -type d";
		findCmd = "cd " + vobroot + ";ls";
	}

	protected IStatus start(IProgressMonitor arg0) {

		List<String> dirList = new ArrayList<String>();
		if (folderFullOSPath == null) {
			SSLhelper sslHelper = new SSLhelper(csession);
			Response res = sslHelper.executeCT(findCmd);
			int status = res.getStatus();
			if (status == 0) {
				if(res.getContent() == null){
					return Status.CANCEL_STATUS;
				}
				String con = res.getContent().toString();
				String[] dirs = con.split("\n");

				for (String d : dirs) {
					String tempPath = csession.getVobRoot() + "/" + d;
					dirList.add(tempPath.replaceAll("/{2,}", "/"));
				}

			} else {
				return Status.CANCEL_STATUS;
			}
		} else {
			IProject currentProject = ReourceHelper.getProject();
			String projectPath = currentProject.getFullPath().toOSString()+File.separator+Constants.VOB;
			String tempFolder = folderFullOSPath.substring(projectPath.length());
			String vobFolderPath = csession.getVobRoot() + "/" + tempFolder.replaceAll("\\\\", "/");
			dirList.add(vobFolderPath.replaceAll("/{2,}", "/"));
		}

		Display.getDefault().asyncExec(new DetailRunnabel(dirList));

		return Status.OK_STATUS;
	}

	static class DetailRunnabel implements Runnable {
		List<String> dirList;

		public DetailRunnabel(List<String> dirList) {
			this.dirList = dirList;
		}

		public void run() {
			DetailVobForm inst = new DetailVobForm(new Shell(
					Display.getDefault()), SWT.NULL);
			// inst.setValues(dirList);
			inst.open(dirList);

			String regexMessage = inst.getRegexMessage();

			if (regexMessage == null) {
				return;
			}

			IProject currentProject = ReourceHelper.getProject();
			ReourceHelper resource = ReourceHelper.getResource();
			IProject project = ReourceHelper.getProject();
			CSession session = resource.getSession(project.getName());

			List<String> downloadDirs = session.getVobWorkDirs();
			if (downloadDirs != null && downloadDirs.size() > 0) {

				int total = downloadDirs.size();
				for (int i = 0; i < total; i++) {
					String dir = downloadDirs.get(i);
					SyncAllJob recevieCodes = new SyncAllJob(
							(" receiving " + dir), currentProject, dir,
							regexMessage);
					recevieCodes.schedule();
				}
			} else {
				LogUtil.err("source directory not be found!");
			}

			String confdir = currentProject.getLocation().toOSString();
			String confFile = confdir + File.separator + PropHelper.CONFIG_FILE;
			File file = new File(confFile);
			if (file.exists()) {
				PropHelper.writeProperties(confFile, PropHelper.SYNC_ALL_REGEX,
						regexMessage);
			}

		}
	}

}
